package com.mall.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.db.domain.Coupon;
import com.mall.db.service.CouponService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

public class CouponTimeJob implements BaseJob{
    @Resource(name = "couponService")
    private CouponService cs;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("timeType",1);
        List<Coupon> list = cs.list(qw);//查询指定绝对时间的优惠券
        for (Coupon c:list) {
            long  EffectiveTime = c.getEndTime().getTime() - c.getStartTime().getTime();//优惠券有效时间
            long nowTime = new Date().getTime() - c.getStartTime().getTime();//现在的时间
            if (nowTime >= EffectiveTime){//当现在的时间超过了优惠券最后的有效时间
                c.setState("1");
                cs.updateById(c);//数据修改这个优惠券的状态
            }
        }
    }
}
